﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XianboSite.Models;
using XianboSite.Common;

namespace XianboSite.Controllers
{
    public class LiveController : Controller
    {
        //
        // GET: /Live/
        public ActionResult Index(string name)
        {
            List<SelectListItem> TempSelectListItem = new List<SelectListItem>();
            IList<LiveCategory> categories = DataAccess.GetLiveCategories();
            foreach (LiveCategory item in categories)
            {
                TempSelectListItem.Add(new SelectListItem()
                {
                    Selected = false,
                    Text = item.CategoryName,
                    Value = item.CategoryName
                });
            }
            ViewData["category"] = TempSelectListItem;
            ViewData["categories"] = DataAccess.GetLiveCategories();
            XianboSite.Models.User user = HttpContext.Application["current_user"] as XianboSite.Models.User;
            if (user != null)
            {
                ViewData["live"] = DataAccess.GetUserLives(user.UserID);
                return View();
            }

            return View();
        }

        public ActionResult CategoryLive(string name)
        {
            ViewData["categories"] = DataAccess.GetLiveCategories();
            ViewData["categoryname"] = name;
            ViewData["live"] = DataAccess.GetLives(name);
            return View();
        }

        public ActionResult CreateChannel()
        {
            List<SelectListItem> TempSelectListItem = new List<SelectListItem>();
            IList<LiveCategory> categories = DataAccess.GetLiveCategories();
            foreach (LiveCategory item in categories)
            {
                TempSelectListItem.Add(new SelectListItem()
                {
                    Selected = false,
                    Text = item.CategoryName,
                    Value = item.CategoryID
                });
            }
            ViewData["category"] = TempSelectListItem;
            return View();
        }

        [ValidateInput(false)]
        public ActionResult Create(FormCollection collection)
        {
            string category = collection["category"];
            string livename = collection["tbLiveName"];
            Live live = new Live()
            {
                CategoryName = category,
                LiveID = RndIDHelper.GetRnd(10, true, true, true, false, string.Empty),
                UserID = "abcd123456",
                LiveName = livename,
                CreateTime = DateTime.Now
            };
            DataAccess.CreateLive(live);
            return Redirect("/LiveDetailsInfo/Index/" + live.LiveID);
        }

    }
}
